package com.adayo.btsppsdk.utils;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;


import com.adayo.btsppsdk.BtReceiver;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by M02323 on 2017/8/2.
 */

public class BluetoothKit {

	private static BluetoothKit instance;
	private Context context;
	private BluetoothAdapter btAdapter = null;
	private BtReceiver receiver;

	private BluetoothKit() {

	}

	public static BluetoothKit getInstance() {
		if (instance == null) {
			instance = new BluetoothKit();
		}
		return instance;
	}

	public void init(Context context) {
		this.context = context;
		btAdapter = initBluetoothAdapter();
	}

	@SuppressLint("NewApi")
	private BluetoothAdapter initBluetoothAdapter() {
		final int sdk = Build.VERSION.SDK_INT;
		PackageManager manager = context.getPackageManager();
		if ((sdk >= 18) && (manager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))) {
			final BluetoothManager bluetoothManager = (BluetoothManager) context
					.getSystemService(Context.BLUETOOTH_SERVICE);
			return bluetoothManager.getAdapter();
		} else {
			return BluetoothAdapter.getDefaultAdapter();
		}
	}

	public void registerBtReceiver() {
		if (!isEnabled()) {
			enable();
		}
		receiver = new BtReceiver();
		context.registerReceiver(receiver, receiver.getFilter());
	}

	public void unregisterBtReceiver() {
		context.unregisterReceiver(receiver);
	}

	public void setOnBtReceiverListener(BtReceiver.OnBtReceiverListener listener) {
		if (receiver != null) {
			receiver.setOnBtReceiverListener(listener);
		}
	}

	public boolean isEnabled() {
		return btAdapter == null ? false : btAdapter.isEnabled();
	}

	public boolean enable() {
		return btAdapter == null ? false : btAdapter.enable();
	}

	public List<BluetoothDevice> getBondedDevices() {
		if (btAdapter == null)
			return null;
		List<BluetoothDevice> devices = new ArrayList<>();
		for (BluetoothDevice device : btAdapter.getBondedDevices()) {
			devices.add(device);
		}
		return devices;
	}

	public boolean isDiscovering() {
		return btAdapter == null ? false : btAdapter.isDiscovering();
	}

	public BluetoothDevice getRemoteDevice(String address) {
		return btAdapter == null ? null : btAdapter.getRemoteDevice(address);
	}

	/**
	 * 开始搜索
	 */
	public void startDiscovery() {
		if (btAdapter != null && !btAdapter.isDiscovering()) {
			JLogKit.getInstance().i("....startDiscovery");
			btAdapter.startDiscovery();
		}
	}

	/**
	 * 取消搜索
	 */
	public void cancelDiscovery() {
		if (isDiscovering()) {
			JLogKit.getInstance().i("....cancelDiscovery");
			btAdapter.cancelDiscovery();
		}
	}

	/**
	 * 配对设备
	 *
	 * @param device 配对蓝牙设备
	 */
	public void pairDevice(BluetoothDevice device) {
		try {
			ClsUtils.createBond(device.getClass(), device);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public BluetoothAdapter getBluetoothAdapter() {
		return btAdapter;
	}
}
